CGI and AppleScript [The following is excerpted from the article "CGI and AppleScript," Dr. Dobb's Sourcebook, Nov./Dec. 1995.] On the Macintosh, CGI applications can be written using AppleScript, the built-in scripting language of the Macintosh operating system. AppleScript was designed as a high-level, dynamic language to provide a systemwide scripting mechanism, always available, which can reach out to existing applications. By far, AppleScriptÕs most powerful capability is being able to communicate with and control other applications, including well over 100 off-the-shelf scriptable applications. This allows you to quickly put together scenarios, simple or complex. Within an AppleScript CGI, you can process data or instructions entered into a form. You can initiate a database search, assembly of text, produce charts or graphics, all driven from user choices. In addition, thereÕs sending e-mail, assembling new HTML for use in creating subsequent Web pages, and dealing with runtime errors. AppleScript works its magic through application-specific vocabularies, which are housed within scriptable applications. A vocabulary extends the language to include new terms representing actions and objects specific to the particular application. Together with AppleScriptÕs built-in terms, you write scripts by putting together sentences, often grammatically correct. A sample AppleScript CGI Suppose you maintain a regularly-updated database of information in a database that includes a table of numeric values, and you want to provide, on demand to your Web users, the table in the form of a chart. This example uses three familiar applications: FileMaker Pro (to hold the data), DeltaGraph Pro (to make a chart of the data), and Clip2Gif (to store the chart in a GIF file). You can use redirection (ÒLocationÓ in the reply header) to point to the Web server to the GIF file. The following is intended as inspiration, not as a working sample. property crlf : (ASCII character 13) & (ASCII character 10) property reply_header : "HTTP/1.0 302 FOUND" & crlf &  "Server: WebSTAR/1.0 ID/ACGI" & crlf &  "Location: http://www.your.site/home.html" & crlf &  "URI: http://www.your.site/home.html" & crlf & crlf property error_header : "HTTP/1.0 200 OK" & crlf &  "Server: WebSTAR/1.0 ID/ACGI" & crlf &  "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf -- This is the CGI Handler on Çevent WWW½sdocÈ path_args  given Çclass postÈ:post_args, Çclass methÈ:method, Çclass addrÈ:client_address try -- Put the body of your CGI here. -- interpret CGI arguments -- Here you would obtain the userÕs choices from the CGI arguments. -- I leave this as an exercise for the reader. -- retrieve data from database tell application "FileMaker Pro" Open alias "Macintosh HD:Databases folder:Web DataBase" set numRecs to Count of Record in Document 1 -- make tab-delimited data for Deltagraph set dataString to "Month" & tab & "Amount" & return repeat with i from 1 to numRecs copy dataString & Cell "month" of Record i & tab &  Cell "amount" of Record i & return to dataString end repeat end tell -- create the desired chart tell application "Deltagraph Pro" Data dataString Plot Options Text Font "Palatino" Text Size 12 Colorstyle "blue" Set Axis Lengths for X 100 for Y 100 Output PICT set dataChart to Plot chart chartType end tell -- make the GIF file for the redirect tell application "clip2gif" save dataChart as GIF in file "Macintosh HD:home.html" end tell -- return the reply data to the server and exit the CGI handler return reply_header -- reply and exit -- If you get an error, the error handling mechanism will drop you in here: on error errNum number errMsg -- Create a page of HTML text to return. set return_page to error_header  &  "Error Page" & "

Error Encountered!

" & return  & "An error was encountered while trying to run this script." & return set return_page to return_page  & "

Error Message

" & return & errMsg & return  & "

Error Number

" & return & errNum & return  & "

Date

" & return & (current date) & return set return_page to return_page & Â "
Please notify the webmaster at " & Â "mailto:webmaster@your.site.com" & " of this error." & "" -- Return the error page created and exit the handler. return return_page end try end Çevent WWW½sdocÈ -- end of handler